#include "index.h"
#include "ui_index.h"
#include <QTableWidget>
#include <QTableWidgetItem>
index::index(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::index)
{
    ui->setupUi(this);
    connect(ui->patientTable,&QTableWidget::cellClicked,this,&index::showPatient);
    connect(ui->searchBtn,&QPushButton::clicked,this,&index::search);
    connect(ui->patientBtn,&QPushButton::clicked,this,&index::showThisPatient);
    connect(ui->newBtn,&QPushButton::clicked,this,&index::newPatient);
    connect(ui->refreshBtn,&QPushButton::clicked,this,&index::refresh);
    pwin=new class showPatient("");
    cwin=new showCase;
    connect(cwin,&showCase::saved,this,&index::refresh);
}

void index::refresh()
{
    l->refreshR10();
    this->setList(l->recent10,l->cntr);
    ui->searchInput->setText("");
}

void index::newPatient()
{
    QDate date = QDate::currentDate();
    QString pp[30],mm[30];
    pp[0]=date.toString("yyyy.MM.dd");
    l->addcase(pp,mm,0);
    cwin->init(l,l->a.length()-1);
    cwin->show();
}

void index::showThisPatient()
{
    qDebug()<<"deleting pwin";
    delete pwin;
    qDebug()<<"new pwin";
    pwin=new class showPatient(list[selected_Patient]["ID"]);
    pwin->setillcases(l);
    pwin->show();
    pwin->setcwin(cwin);
}

void index::search()
{
    QString s=ui->searchInput->text();
    qDebug()<<s;

    if(s.length()==0)
    {
        this->setList(l->recent10,l->cntr);
        return;
    }
    bool isName=true;
    for(int i=0;i<s.length();i++)
    {
        if(s[i]>='0'&&s[i]<='9')
        {
            isName=false;
            break;
        }

    }
    illcase lis[1000];
    int nn;
    if(isName)
    {
        nn=l->askpersonByName(s,lis);
    }
    else
    {
        nn=l->askillcasesByID(s,lis);
    }
    qDebug()<<lis[0]["name"];
    this->setList(lis,nn);
}

void index::showPatient(int x, int y)
{
    selected_Patient=x;
    illcase l=list[x];
    ui->patientLabel->setText(  "日期:\t\t"+l["date"]+
                                "\n医院:\t\t"+l["hospital"]+
                                "\n姓名:\t\t"+l["name"]+
                                "\n性别:\t\t"+l["sex"]+
                                "\n医保类型:\t"+l["health_insurance_type"]+
                                "\n门诊号:\t\t"+l["ID"]+
                                "\n年龄:\t\t"+l["age"]+
                                "\n主症:\t\t"+l["main_symptom"]+
                                "\n诊断:\t\t"+l["diagnosis"]);
}

void index::setillcases(illcases *p)
{
    l=p;
    this->setList(l->recent10,l->cntr);
}

void index::setList(illcase a[], int n)
{
    qDebug()<<"size of list:"<<n;
    ui->patientTable->setRowCount(n);
    for(int i=0;i<n;i++)
    {
        list[i]=a[i];
        QTableWidgetItem *p[6];
        for(int j=0;j<5;j++)
            p[j]=new QTableWidgetItem;

        p[0]->setText(list[i]["name"]);
        p[1]->setText(list[i]["sex"]);
        p[2]->setText(list[i]["health_insurance_type"]);
        p[3]->setText(list[i]["ID"]);
        p[4]->setText(list[i]["age"]);

        for(int j=0;j<5;j++)
            ui->patientTable->setItem(i,j,p[j]);
    }

}

index::~index()
{
    delete ui;
}
